package com.elitel.frame.base.component.quartz;

import com.elitel.frame.base.service.BaseQueryfieldcacheService;
import com.elitel.frame.base.service.CfgExtendService;
import com.elitel.frame.base.service.SysDocumentService;
import com.elitel.frame.main.config.properties.BaseFrameProperties;
import com.elitel.frame.main.dao.ext.ExtCfgExtsearchMapper;
import com.elitel.frame.main.entity.CfgExtend;
import com.elitel.frame.main.entity.CfgExtsearch;
import com.elitel.frame.main.entity.SysDocument;
import com.elitel.frame.main.entity.ext.BaseQueryfieldcacheExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * created by guoyanfei on 2018/3/21
 */
@Component("searchServiceTask")
public class SearchServiceTask {
    @Autowired
    private ExtCfgExtsearchMapper extCfgExtsearchMapper;
    @Autowired
    private CfgExtendService cfgExtendService;
    @Autowired
    private QuartzScheduler quartzScheduler;
    @Autowired
    private SysDocumentService sysDocumentService;
    @Autowired
    private BaseQueryfieldcacheService baseQueryfieldcacheService;
    @Autowired
    private BaseFrameProperties baseFrameProperties;

    /**
     * 创建全文检索服务更新elasticsearch索引库的任务调度<br/>
     * serviceId如为空，设置所有的全文检索服务调度任务，否则就设置当前传过来的服务的调度任务
     * created by guoyanfei on 2018/3/21
     *
     * @param serviceId 全文检索服务ID
     */
    public void startSearchServiceScheduler(String serviceId) {
        List<CfgExtsearch> cfgExtsearches = extCfgExtsearchMapper.selectCfgExtsearchByserviceId(serviceId);
        if (cfgExtsearches != null && cfgExtsearches.size() > 0) {
            for (CfgExtsearch cfgExtsearch : cfgExtsearches) {
                quartzScheduler.addJob(cfgExtsearch.getGuid(),
                        SearchServiceQuartz.class, cfgExtsearch.getCronexpression());
            }
            quartzScheduler.startJobs();
        }

    }

    /**
     * 修改任务时间
     * created by guoyanfei on 2018/03/21
     *
     * @param serviceId 服务ID
     * @param time      修改时间
     */
    public void updateSchedulerTime(String serviceId, String time) {
        quartzScheduler.updateJobTime(serviceId, time);
    }

    /**
     * 从任务调度中移除任务
     * create on 2018/3/22
     *
     * @param serviceId 服务ID
     * @author guoyanfei
     */
    public void removeTask(String serviceId) {
        quartzScheduler.removeJob(serviceId);
    }

    /**
     * 创建非结构化文档全文检索服务更新elasticsearch索引库的任务调度<br/>
     * procode如为空，设置所有的非结构化文档全文检索服务调度任务，否则就设置当前传过来的服务的调度任务
     * created by guoyanfei on 2018/04/19
     *
     * @param serviceId 服务ID
     */
    public void startSearchDocumentScheduler(String serviceId) {
        List<SysDocument> sysDocumentList = sysDocumentService.getSysDocumentListByserviceId(serviceId);
        if (sysDocumentList != null && sysDocumentList.size() > 0) {
            for (SysDocument sysDocument : sysDocumentList) {
                quartzScheduler.addJob(sysDocument.getGuid(), SearchDocumentQuartz.class, sysDocument.getCronexpression());
            }
            quartzScheduler.startJobs();
        }
    }

    /**
     * 创建热配置数据缓存服务刷新Redis缓存调度
     * 如果serviceId serviceType groupId为空   则查询所有的
     *
     * @param serviceId
     * @param serviceType
     * @param groupId
     */
    public void startQueryDataByCacheConfigScheduler(String serviceId, String serviceType, String groupId) {
        List<BaseQueryfieldcacheExt> grouplist = baseQueryfieldcacheService.queryAllFieldCacheGroup(serviceId, serviceType, groupId);
        if (grouplist == null && grouplist.size() <= 0)
            return;
        for (BaseQueryfieldcacheExt fieldcache : grouplist) {
            CfgExtend cfgExtend = cfgExtendService.selectByPrimaryKey(fieldcache.getServiceid());
            if (cfgExtend == null) {
                return;
            }
            Integer catchtime = cfgExtend.getCatchtime();
            catchtime = catchtime == null ? 1 : catchtime;
            //String time = "5 0/2 18-22 * * ?";
            //String time = "0 0/" + String.valueOf(catchtime - 1).trim() + " * * * ?";
            //String time = "0 0/2 9-18 * * ?";
            String jobname = QuartzConfigUtil.getJobName(fieldcache.getServiceid(), String.valueOf(fieldcache.getServicetype()), fieldcache.getParametergroup());
            quartzScheduler.removeJob(fieldcache.getServiceid(), jobname);
            quartzScheduler.addJob(fieldcache.getServiceid(), jobname, QueryFieldCacheQuartz.class, ((catchtime) * 60 - baseFrameProperties.getFieldcacheovertime()));
        }
        quartzScheduler.startJobs();
    }

}
